package com.dsg.service.apply.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.dao.apply.DsgApplyStatusMapper;
import com.dsg.entity.apply.DsgApplyStatus;
import com.dsg.entity.approve.DsgApproveCase;
import com.dsg.entity.dto.ApplyStatusDto;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.apply.IDsgApplyStatusService;
import com.dsg.service.approve.IDsgApproveCaseService;
import com.dsg.util.RRException;
import com.dsg.workflow.WorkflowEngine;
import com.dsg.workflow.WorkflowService3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 業務申請單状态 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-08-07
 */
@Service
public class DsgApplyStatusServiceImpl extends DsgServiceImpl<DsgApplyStatusMapper, DsgApplyStatus> implements IDsgApplyStatusService {
    @Autowired
    private IDsgApproveCaseService caseService;
    @Autowired
    private WorkflowEngine engine;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDsgApplyStatus(String idList, Integer checkStatus) {
        if (StringUtil.isEmpty(idList)) {
            throw new RRException("主鍵不能為空！");
        }
        QueryWrapper<DsgApplyStatus> queryWrapper = new QueryWrapper();
        queryWrapper.in("OID", Arrays.asList(idList.split(",")));
        List<DsgApplyStatus> dsgApplyStatusList = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(dsgApplyStatusList)) {
            List<String> appNoList = dsgApplyStatusList.stream().map(I -> I.getAppNo()).collect(Collectors.toList());
            queryWrapper.clear();
            queryWrapper.in("APP_NO", appNoList);
            dsgApplyStatusList = list(queryWrapper);
            Map<String, List<DsgApplyStatus>> applyStatusMap = dsgApplyStatusList.stream().collect(Collectors.groupingBy(I -> I.getAppNo()));
            List<DsgApplyStatus> upDsgApplyStatus = new ArrayList<>();
            applyStatusMap.forEach((key, I) -> {
                if (DsgApplyStatus.AppStatus.success.getCode().intValue() == checkStatus) {
                    DsgApproveCase approveCase = caseService.getCase(WorkflowService3.NODE_CHECK_CAR_NO, key);
                    if (approveCase == null) {
                        throw new RRException("對單CASE不存在");
                    }
                    try {
                        engine.forward(approveCase);
                    } catch (Exception e) {
                        log.error("推實例異常：", e);
                        throw new RRException("推實例異常");
                    }
                    I.forEach(J -> {
                        J.setCheckStatus(checkStatus);
                        J.setIsCheck(DsgApplyStatus.AppStatus.yes.getCode());
                        upDsgApplyStatus.add(J);
                    });
                }
            });
            updateBatchById(upDsgApplyStatus);
        }
    }

    @Override
    public void setIsMatch(String appNo) {
        LambdaUpdateWrapper<DsgApplyStatus> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(DsgApplyStatus::getAppNo, appNo);
        wrapper.set(DsgApplyStatus::getIsMatch, 1);
        update(new DsgApplyStatus(), wrapper);
    }

    @Override
    public List<ApplyStatusDto> getDsgApplyStatusList(ApplyStatusDto applyStatusDto) {
        return baseMapper.getDsgApplyStatusList(applyStatusDto);
    }
}
